﻿using AutoMapper;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Interfaces;

namespace SmartConference.Api.Applocations.Services
{
    public class RoleServices : IRoleServices
    {
        private readonly IRoleRepository roleRepository;
        private readonly IMapper mapper;

        public RoleServices(IRoleRepository roleRepository,IMapper mapper)
        {
            this.roleRepository = roleRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> AddRole(AddRoleDTO addRoleDTO)
        {
            try
            {
                var list=await roleRepository.GetAsync(x => x.RoleName == addRoleDTO.RoleName);
                if (list != null)
                {
                    return ApiResult.Error(ApiResultCode.Error, "职务已存在");
                }
                else
                {
                    var role = mapper.Map<RoleInfo>(addRoleDTO);
                    role.RoleStatus=RoleStatus.Enabled;
                    role.MemberCount = 100;
                    await roleRepository.AddAsync(role);
                    return ApiResult.Success(ApiResultCode.Success);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult> DeleteRole(int RoleId)
        {
            try
            {
                var list = await roleRepository.GetAsync(x => x.Id == RoleId);
                await roleRepository.DeleteAsync(list);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult<List<RoleInfo>>> GetRole(string? RoleName)
        {
            var rolelist = roleRepository.GetAll();
            if (!string.IsNullOrEmpty(RoleName))
            {
                rolelist = rolelist.Where(x => x.RoleName.Contains(RoleName));
            }
            return ApiResult<List<RoleInfo>>.Success(ApiResultCode.Success, rolelist.ToList());
        }

        public async Task<ApiResult> UpdateRole(int Roleid, string RoleName, string DepartmentDescription)
        {
            try
            {
                var list=await roleRepository.GetAsync(x => x.Id == Roleid);
                list.RoleName = RoleName;
                list.DepartmentDescription = DepartmentDescription;
                await roleRepository.UpdateAsync(list);
                return ApiResult.Success(ApiResultCode.Success);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<ApiResult> UpdateRoleState(int RoleId, int RoleStatus, string ContentDesc)
        {
            var list = await roleRepository.GetAsync(x => x.Id == RoleId);
            list.RoleStatus = (RoleStatus)RoleStatus;
            list.ContentDesc = ContentDesc;
            await roleRepository.UpdateAsync(list);
            return ApiResult.Success(ApiResultCode.Success);
        }


    }
}
